package com.ruoyi.web.controller.project.software;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.software.SoftwareCopyright;
import com.ruoyi.system.domain.project.software.SoftwareCopyrightTeacher;
import com.ruoyi.system.domain.project.software.SoftwareVo;
import com.ruoyi.system.service.project.ISoftwareCopyrightService;
import com.ruoyi.system.service.project.ISoftwareCopyrightTeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 软件著作权信息，存储智能药房系统V1.1等著作权登记详情Controller
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/project/copyright")
public class SoftwareCopyrightController extends BaseController
{
    @Autowired
    private ISoftwareCopyrightService softwareCopyrightService;
    @Autowired
    private ISoftwareCopyrightTeacherService softwareCopyrightTeacherService;

    /**
     * 查询软件著作权信息，存储智能药房系统V1.1等著作权登记详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SoftwareCopyright softwareCopyright)
    {
        startPage();
        List<SoftwareCopyright> list = softwareCopyrightService.selectSoftwareCopyrightList(softwareCopyright);
        return getDataTable(list);
    }

    /**
     * 导出软件著作权信息，存储智能药房系统V1.1等著作权登记详情列表
     */
    @Log(title = "软件著作权信息，存储智能药房系统V1.1等著作权登记详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SoftwareCopyright softwareCopyright,
                       @RequestParam(required = false) String exportFields)
    {
        List<SoftwareCopyright> list = softwareCopyrightService.selectSoftwareCopyrightList(softwareCopyright);
        List<SoftwareVo> voList = new java.util.ArrayList<>();
        for (SoftwareCopyright info : list){
            if (info.getTeachers() != null && !info.getTeachers().isEmpty()) {
                for (SoftwareCopyrightTeacher teacher : info.getTeachers()){
                    SoftwareVo vo = new SoftwareVo();
                    // 基本属性映射
                    vo.setCertificateNumber(info.getCertificateNumber());
                    vo.setSoftwareName(info.getSoftwareName());
                    vo.setCopyrightHolder(info.getCopyrightHolder());
                    vo.setRightObtainingMethod(info.getRightObtainingMethod());
                    vo.setRightScope(info.getRightScope());
                    vo.setRegistrationNumber(info.getRegistrationNumber());
                    vo.setApprovalBasis(info.getApprovalBasis());
                    vo.setApprovalOrganization(info.getApprovalOrganization());
                    vo.setRegistrationDate(info.getRegistrationDate());
                    vo.setUserName(teacher.getUserName());
                    voList.add( vo);
                }
            }
        }
        ExcelUtil<SoftwareVo> util = new ExcelUtil<SoftwareVo>(SoftwareVo.class);
        // 如果传递了导出字段，则进行处理
        if (StringUtils.isNotBlank(exportFields)) {
            // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
            String[] fields = exportFields.split(",");
            util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
        }
        util.exportExcel(response, voList, "软件著作权信息，存储智能药房系统V1.1等著作权登记详情数据");
    }

    /**
     * 获取软件著作权信息，存储智能药房系统V1.1等著作权登记详情详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(softwareCopyrightService.selectSoftwareCopyrightById(id));
    }

    /**
     * 新增软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     */
    @Log(title = "软件著作权信息，存储智能药房系统V1.1等著作权登记详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SoftwareCopyright softwareCopyright)
    {
        SoftwareCopyrightTeacher softwareCopyrightTeacher = new SoftwareCopyrightTeacher();
        if(softwareCopyright.getId()!=null){
            softwareCopyrightTeacher.setResearchId(softwareCopyright.getId());
        }else{
            softwareCopyrightTeacher.setResearchId(-1L);
        }
        List<SoftwareCopyrightTeacher> list = softwareCopyrightTeacherService.selectSoftwareCopyrightTeacherList(softwareCopyrightTeacher);
        Long currentUserId = softwareCopyright.getUserId();
        if(!list.isEmpty()){
            for (SoftwareCopyrightTeacher softwareCopyrightTeacher1 : list) {
                if(softwareCopyrightTeacher1.getUserId().equals(String.valueOf(currentUserId))){
                    return error("此软件著作您已经参与并有名次记录，请勿重复添加");
                }
            }
        }
        return toAjax(softwareCopyrightService.insertSoftwareCopyright(softwareCopyright));
    }

    /**
     * 修改软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     */
    @Log(title = "软件著作权信息，存储智能药房系统V1.1等著作权登记详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SoftwareCopyright softwareCopyright)
    {
        return toAjax(softwareCopyrightService.updateSoftwareCopyright(softwareCopyright));
    }

    /**
     * 删除软件著作权信息，存储智能药房系统V1.1等著作权登记详情
     */
    @Log(title = "软件著作权信息，存储智能药房系统V1.1等著作权登记详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(softwareCopyrightService.deleteSoftwareCopyrightByIds(ids));
    }

    /**
     * 根据关键字查询
     */
    @GetMapping("/getCopyrightInfoByCertificateNumber")
    public AjaxResult getCopyrightInfoByCertificateNumber(  String data)
    {
        SoftwareCopyright ra = softwareCopyrightService.getCopyrightInfoByCertificateNumber(data);
        return success(ra);
    }
}
